﻿using Common;
using Service.DtoModel;
using Service.DtoModel.DeskApp;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Service.Implements
{
    public class BillConsumeService : BaseServer<BillConsume>
    {
        /// <summary>
        /// 获取订单信息
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<BillConsume>>> QueryList(PageParm parm)
        {
            var res = new ApiResult<List<BillConsume>>() { statusCode = (int)ApiEnum.Error };
            try
            {
                var parms = new List<SugarParameter>() { new SugarParameter("@obId", parm.key) };
                StringBuilder sql = new StringBuilder(" SELECT * FROM BillConsume ");
                if (!string.IsNullOrEmpty(parm.key))
                {
                    sql.Append(" where obId like '%@obId%' ");
                }
                var _TB = Db.Ado.GetDataTable(sql.ToString(), parms);
                List<BillConsume> _datas = new ModelHandler<BillConsume>().FillModel(_TB);
                res.data = _datas;
                res.statusCode = (int)ApiEnum.Status;
            }
            catch (Exception ex)
            {
                res.message = ex.Message;
                res.success = false;
                Logger.Default.ProcessError((int)ApiEnum.Error, ex.Message);
            }
            return res;
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task<ApiResult<Page<BillConsume>>> GetPagesAsync(PageParm parm)
        {
            var res = new ApiResult<Page<BillConsume>>();
            try
            {
                res.data = await Db.Queryable<BillConsume>()
                                   .WhereIF(!string.IsNullOrEmpty(parm.key), m => m.obId.Contains(parm.key))
                                   .OrderBy(m => m.msTime, OrderByType.Desc)
                                   .ToPageAsync(parm.page, parm.limit);
                res.statusCode = (int)ApiEnum.Status;
            }
            catch (Exception ex)
            {
                res.statusCode = (int)ApiEnum.Error;
                Logger.Default.ProcessError((int)ApiEnum.Error, ex.Message);
            }
            return res;
        }
        /// <summary>
        /// 获取已开单的销售数据
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task<ApiResult<Page<BillConsumeDto>>> GetJoinPagesAsync(PageParm parm)
        {
            var res = new ApiResult<Page<BillConsumeDto>>();
            try
            {
                /*
                 SELECT a.rName,a.obId,a.rtNumber,a.rtState,rtdate,(SELECT SUM(msAmount*msMoney) FROM billconsume WHERE obid=a.obid) cost
                 FROM rooms a WHERE a.rtstate='占用' GROUP BY a.obid 
                 */
                var _list = Db.Queryable<BillConsume, Room>((orderInfo, tableInfo)
                    => new object[] { JoinType.Left, orderInfo.obId == tableInfo.obId && tableInfo.rtState == "占用" })
                    .Select((orderInfo, tableInfo) => new BillConsumeDto
                    {
                        TabNo = tableInfo.rName,
                        id = orderInfo.id,
                        obId = orderInfo.obId,
                        msAmount = orderInfo.msAmount,
                        msMoney = orderInfo.msMoney,
                        msTime = orderInfo.msTime,
                        msId = orderInfo.msId,
                        rtNumber = tableInfo.rtNumber,
                        costAmount = orderInfo.msAmount * orderInfo.msMoney
                    }).MergeTable();
                res.data = await _list.WhereIF(!string.IsNullOrEmpty(parm.key), m => m.obId.Contains(parm.key))
                                      .OrderBy(m => m.msTime, OrderByType.Desc)
                                      .ToPageAsync(parm.page, parm.limit);
                res.statusCode = (int)ApiEnum.Status;
            }
            catch (Exception ex)
            {
                res.statusCode = (int)ApiEnum.Error;
                Logger.Default.ProcessError((int)ApiEnum.Error, ex.Message);
            }
            return res;
        }

        /// <summary>
        /// 根据主键删除数据
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task<ApiResult<string>> DeleteAsync(string parm)
        {
            var list = Utils.StrToListString(parm);
            var isok = await Db.Deleteable<Menu>().Where(m => list.Contains(m.msId.ToString())).ExecuteCommandAsync();
            var res = new ApiResult<string>
            {
                success = isok > 0,
                statusCode = isok > 0 ? (int)ApiEnum.Status : (int)ApiEnum.Error,
                data = isok > 0 ? "1" : "0"
            };
            return res;
        }
    }
}
